<?php
// +----------------------------------------------------------------------
// | Description: 1
// +----------------------------------------------------------------------
// | Author: orchief
// +----------------------------------------------------------------------
// | Date: 2018-03-02 14:19:49
// +----------------------------------------------------------------------

namespace app\client\controller;

use app\common\controller\Rest;
use think\Db;

class Cashes extends Rest
{
    public $modelName = 'Cash';
    public function _initialize()
    {
        parent::_initialize();
        // $this -> abort(!$this -> isClient, 'Permission denied', 403);
    }
    public function save()
    {
        $bankId = $this -> param['BankId'];
        $this -> abort($bankId, '请选择银行卡');
        $info = Db::table('shop_banks') -> where(['Id' => $bankId]) -> find();
        $data['BankCard'] = $info['BankCard'];
        $data['Cardholder'] = $info['Cardholder'];
        $data['OpeningBank'] = $info['OpeningBank'];
        $data['BankName'] = $info['BankName'];
        $data['Money'] = $this -> param['Money'];
        $data['UserId'] = $this -> param['UserId'];
        // 生成账户记录
        Db::startTrans();
        try{
            $this -> accountRecord($this -> param['Money']);
            $res = self::$model->createData($data);
            Db::commit();
        }catch(\Exception $e){
            Db::rollback();
            $this -> abort(false, '余额不足！');
        }
        
        $this->checkRes($res, '添加成功', self::$model->getError());
    }
    
    public function update()
    {
        $this -> abort($this->param['Applied'], '非法操作');
        $Id = $this->param['id'];
        switch($this->param['Applied']){
            case 1:
                self::$model -> updateDataById(['Applied' => 1], $Id);   // 申请成功不生成额外记录了
                break;
            case 2:
                self::$model -> updateDataById(['Applied' => 2], $Id);
                $this -> accountReject($Id);
                break;
            default:
                $this -> abort(false, '非法操作');
            break;
        }
        $res = self::$model->updateDataById($this->param, $Id);
        $this->checkRes($res, '编辑成功', self::$model->getError());
    }

    private function accountReject($Id)
    {
        $info = self::$model -> where(['Id' => $Id]) -> find();
        $Money = $info['Money'];
        $balance = Db::table('shop_members') -> where(['Id' => $info['UserId']]) -> field('Balance') -> find()['Balance'];
        Db::table('shop_members') -> where(['Id' => $info['UserId']]) -> setInc('Balance', $Money);
        $data['Type'] = '提现失败';
        $data['UserId'] = $this -> param['UserId'];
        $data['Change'] = -$Money;
        $data['Remain'] = $balance + $Money;
        Db::table('shop_members_account') -> insert($data);
    }


    /**
     * 生成账户记录
     *
     * @return void
     */
    private function accountRecord($Money)
    {
        $balance = Db::table('shop_members') -> where(['Id' => $this -> param['UserId']]) -> field('Balance') -> find()['Balance'];
        Db::table('shop_members') -> where(['Id' => $this -> param['UserId']]) -> setDec('Balance', $Money);
        $data['Type'] = '申请提现';
        $data['UserId'] = $this -> param['UserId'];
        $data['Change'] = $Money;
        $data['Remain'] = $balance - $Money;
        Db::table('shop_members_account') -> insert($data);
    }
}
